package com.meiqiongproperty.yiguanjia.ui.adbanner.slidertypes;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.fragment.home.h5.H5UserActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * This is a slider with a description TextView.
 */
public class TextSliderView extends BaseSliderView {
    List<String> img = new ArrayList<>();
    List<String> imgurl;
    List<String> title;
    List<Integer> imgResource = new ArrayList<>();
    Context context;
    boolean isClick = true;

    public TextSliderView(Context context) {
        super(context);
        this.context = context;
    }

    public TextSliderView(Context context, List<String> img) {
        super(context);
        this.context = context;
        this.img = img;
    }

    public TextSliderView(Context context, List<String> img, List<String> imgurl, List<String> title) {
        super(context);
        this.context = context;
        this.img = img;
        this.imgurl = imgurl;
        this.title = title;
    }

    public TextSliderView(Context context, List<String> img, boolean isClick) {
        super(context);
        this.context = context;
        this.img = img;
        this.isClick = isClick;
    }

    public TextSliderView(Context context, List<Integer> imgResource, int type) {
        super(context);
        this.context = context;
        this.imgResource = imgResource;
    }

    @Override
    public View getView(final int position) {
        View v = LayoutInflater.from(getContext()).inflate(R.layout.render_type_text, null);
        ImageView target = (ImageView) v.findViewById(R.id.daimajia_slider_image);
        TextView description = (TextView) v.findViewById(R.id.description);
        description.setText(getDescription());
        if (img.size() != 0) {
            Glide.with(context).load(img.get(position % img.size())
                    .toString()).into(target);
        }

        if (imgResource.size() != 0) {
            Glide.with(context).load(imgResource.get(position % imgResource.size())).into(target);
        }
        //loadImage(target);
        target.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isClick) {
                    if (title!=null){
                        toH5_ad(imgurl.get(position % img.size()).toString(), title.get(position % title.size()).toString());
                    }
                } else {
                }

            }
        });
        bindClickEvent(v);
        return v;
    }

    private void toH5_ad(String url, String title) {
        Intent i = new Intent();
        i.putExtra(H5UserActivity.URL, url);
        i.putExtra(H5UserActivity.TITLE, title);
        i.setClass(getContext(), H5UserActivity.class);
        getContext().startActivity(i);
    }
}
